package com.gnerv.battle.boot.framework.business.impl;

import com.gnerv.battle.boot.framework.business.ManageAccountBusiness;
import com.gnerv.battle.boot.framework.common.model.RestResult;
import com.gnerv.battle.boot.framework.common.tools.UUIDTools;
import com.gnerv.battle.boot.framework.model.bo.ManageAccountBO;
import com.gnerv.battle.boot.framework.model.dto.ManageAccountCreateDTO;
import com.gnerv.battle.boot.framework.model.dto.ManageAccountDTO;
import com.gnerv.battle.boot.framework.model.dto.ManageAccountUpdateDTO;
import com.gnerv.battle.boot.framework.service.ManageAccountService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class ManageAccountBusinessImpl implements ManageAccountBusiness {

    private final ManageAccountService manageAccountService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(ManageAccountCreateDTO manageAccountCreateDTO) {
        String ukId = UUIDTools.lower();
        ManageAccountBO manageAccountBO = manageAccountCreateDTO.toManageAccountBO();
        manageAccountBO.setUkId(ukId);
        manageAccountService.create(manageAccountBO);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        manageAccountService.deleteByUkId(ukId);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(ManageAccountUpdateDTO accountUpdateDTO) {
        ManageAccountBO manageAccountBO = accountUpdateDTO.toManageAccountBO();
        manageAccountService.updateByUkId(manageAccountBO);
        return RestResult.ok();
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<ManageAccountDTO> selectByUkId(String ukId) {
        return RestResult.ok(manageAccountService.selectByUkId(ukId));
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<List<ManageAccountDTO>> listByUsername(String username, Integer page, Integer size) {
        List<ManageAccountDTO> accountDTOList = manageAccountService.listByUsername(username, page, size);
        long count = manageAccountService.countByUsername(username);
        return RestResult.ok(accountDTOList).page(page, size, count);
    }

}
